package com.jb.repair.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jb.repair.R;

/**
 * Created by SLF on 2015/12/17.
 */
public class AutoRelativeLayout extends RelativeLayout implements View.OnClickListener {

    private TextView tv_right;
    private IconFontTextView icon;
    private TextView tv_main;
    private RelativeLayout relativeLayout;
    private OnRightTextClickListener onRightTextClickListener;

    public AutoRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AutoRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public AutoRelativeLayout(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.relativelayout_item, this);
        tv_main = (TextView) this.findViewById(R.id.tv_main);
        icon = (IconFontTextView) this.findViewById(R.id.icon);
        tv_right = (TextView) this.findViewById(R.id.tv_right);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl);
        tv_right.setOnClickListener(this);
        relativeLayout.setOnClickListener(this);
    }

    //设置图标是否可见
    public void setIconVisibility(boolean b) {
        if (!b) {
            icon.setVisibility(INVISIBLE);
        }
    }

    //设置右边文字是否可见
    public void setTvRightVisibility(boolean b) {
        if (!b) {
            tv_right.setVisibility(INVISIBLE);
        }
    }

    //设置左边文字
    public void setTv_main(String text) {
        tv_main.setText(text);
    }

    //设置文字与左边框的距离
    public void setMargin(int width) {
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = width;
        tv_main.setLayoutParams(params);
    }

    //设置背景图片
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setBackgroundStyle(int id) {
        relativeLayout.setBackgroundResource(id);
    }

    public static class OnRightTextClickListener {
        public void OnRightTextClick(View view) {

        }

        public void OnRLClick(View view) {

        }
    }

    @Deprecated
    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(l);
    }

    public void setOnClickListener(OnRightTextClickListener onRightTextClickListener) {
        this.onRightTextClickListener = onRightTextClickListener;
    }

    @Override
    public void onClick(View v) {
        if (onRightTextClickListener == null) {
            return;
        }
        if(v.getId() == R.id.tv_right){

            onRightTextClickListener.OnRightTextClick(v);
        }else if(v.getId() == R.id.rl){
            onRightTextClickListener.OnRLClick(v);
        }
    }
}
